<?php
	include_once("../persistent/BaseObject.php");
	
	class Description extends BaseObject
	{
		public $content;
		public $Bookname;
		public $Thesisname;
		
		public function __construct($d)
		{
			$this->tableName = "Description";
			parent::__construct($d);	
		}		
		
		public function findBookContentlist($N)
		{
			$con = $this->openDB();
			$sql = "SELECT * FROM $this->tableName
			WHERE Thesisname IS NULL
			LIMIT $N";
			
			$description_list = $this->startSQL($sql,$con);
			$content_list = array();
			
			while ($content = mysql_fetch_array($description_list))
				$content_list[] = $content['Content'];

			return $content_list;
		}
		
		public function findThesisContentlist($N)
		{
			$con = $this->openDB();
			$sql = "SELECT * FROM $this->tableName
			WHERE Bookname IS NULL
			LIMIT $N";
			
			$description_list = $this->startSQL($sql,$con);
			$content_list = array();
			
			while ($content = mysql_fetch_array($description_list))
				$content_list[] = $content['Content'];

			return $content_list;
		}
		
		public function findValueByContent($type, $content)
		{
			$con = $this->openDB();
			
			if ($type === "book")
			{
				$sql = "SELECT * FROM $this->tableName
				WHERE Content = '$content' AND Thesisname IS NULL";
			}
			if ($type === "thesis")
			{
				$sql = "SELECT * FROM $this->tableName
				WHERE Content = '$content' AND Bookname IS NULL";
			}
			
			return $this->startSQL($sql,$con);
		}
		
		public function findValueByBookname($bookname)
		{
			$con = $this->openDB();
			
			// 找出Username的条目
			$sql = "SELECT * FROM $this->tableName
			WHERE Bookname = '$bookname'";
			
			return mysql_fetch_array($this->startSQL($sql,$con));
		}
		
		public function findValueByThesisname($thesisname)
		{
			$con = $this->openDB();
			
			// 找出Username的条目
			$sql = "SELECT * FROM $this->tableName
			WHERE Thesisname = '$thesisname'";
			
			return mysql_fetch_array($this->startSQL($sql,$con));
		}
		
		public function findNBooksByDescription($content, $N){
			$list = $this->findValueByContent("book", $content);
			$booknames = array();
			
			while ($value = mysql_fetch_array($list))
				$booknames[] = $value['Bookname'];

			return $booknames;
		}
		
		public function findNThesesByDescription($content, $N){
			$list = $this->findValueByContent("thesis",$content);
			$thesisnames = array();
			
			while ($value = mysql_fetch_array($list))
				$thesisnames[] = $value['Thesisname'];

			return $thesisnames;
		}
	}

?>
